define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'individuality.withdrawal/index' + location.search,
                    add_url: 'individuality.withdrawal/add',
                   // edit_url: 'individuality.withdrawal/edit',
                    del_url: 'individuality.withdrawal/del',
                    multi_url: 'individuality.withdrawal/multi',
                    import_url: 'individuality.withdrawal/import',
                    table: 'individuality_withdrawal',
                }
            });

            var table = $("#table");
            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                //这里我们手动设置底部的值
                if(data.is_individuality==1){
                    table.bootstrapTable('hideColumn','operate');
                }
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                exportTypes: ['excel'],
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        //{field: 'individuality_id', title: __('Individuality_id')},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'remark', title: __('Remark'), operate: 'LIKE', table: table, class: 'autocontent',
                            formatter: function (value, row, index) {
                                return Table.api.formatter.content(value || '').replace(/<img.*?>/gi, '');
                            }
                        },//{field: 'admin_id', title: __('Admin_id')},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'admin.username', title: __('admin_id'), operate: 'LIKE'},
                        {field: 'individuality.name', title: __('Individuality.name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'operate', title: __('Operate'), table: table,
                            buttons:[
                                {
                                    name: 'ok',
                                    title: '通过',
                                    text: '通过',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: '',
                                    confirm: '确定通过审核？',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status == 1 || row.status == 2) {
                                            return false
                                        }
                                        return true;
                                    },
                                    url: function (row, column) {
                                        var url = 'individuality/withdrawal/operation?id=' + row.id + '&status=1';
                                        return url;
                                    },
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                },
                                {
                                    name: 'operation',
                                    classname: 'btn btn-xs btn-info btn-refund',
                                    text: "驳回",
                                    icon: "",
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status == 1 || row.status == 2) {
                                            return false
                                        }
                                        return true;

                                    }
                                },
                            ],
                        events: {
                            'click .btn-refund': function (e, value, row) {
                                Layer.prompt({
                                    title: "请输入备注",
                                    formType: 2,
                                    area: ['200px', '100px'], //自定义文本域宽高
                                    value: row.remark,
                                }, function (value) {
                                    Fast.api.ajax({
                                        url: "individuality/withdrawal/operation?id=" + row.id + '&status=2',
                                        data: {remark: value},
                                    }, function (data, ret) {
                                        Layer.closeAll();
                                        $(".btn-refresh").trigger("click");
                                    });
                                });
                                return false;
                            },
                            'click .btn-delone': function (e, value, row, index) {
                                e.stopPropagation();
                                e.preventDefault();
                                var that = this;
                                var top = $(that).offset().top - $(window).scrollTop();
                                var left = $(that).offset().left - $(window).scrollLeft() - 260;
                                if (top + 154 > $(window).height()) {
                                    top = top - 154;
                                }
                                if ($(window).width() < 480) {
                                    top = left = undefined;
                                }
                                Layer.confirm(
                                    __('Are you sure you want to delete this item?'),
                                    {
                                        icon: 3,
                                        title: __('Warning'),
                                        offset: [top, left],
                                        shadeClose: true,
                                        btn: [__('OK'), __('Cancel')]
                                    },
                                    function (index) {
                                        var table = $(that).closest('table');
                                        var options = table.bootstrapTable('getOptions');
                                        Table.api.multi("del", row[options.pk], table, that);
                                        Layer.close(index);
                                    }
                                );
                            },
                        },formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
